import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
      obj: {}, // 合同obj
      modelShowView : false, // 合同
      checkedUser: {},
      logId: 0,
      logData: [],
    logPage: 1,
    logTotal: 0,
      ModelLog: false,
      userName: '',

      attPage: 1,
      attTotal:0,  // 材料分页条数

      attPage2: 1,
      attTotal2:0,  // 材料分页条数

    constPage: 1,  // 建设
    constTotal: 0,


    expertsPage: 1,  // 专家
      expertsTotal: 0,

      projectPage: 1,
      projectTotal: 0,

      pictureImg: '',
      projectObj: {},
      projectData: [], // 项目table
      ModelConstruction: false, // 基地建设 model
      construction: [], // 基地建设
      temFileUploads: [], // 终止附件
      termination: '', // 基地终止附件
      attData: [], // 验收材料
      tableDatap: [], // 评审
      experts: [], // 专家
      thatUrl: this.urls.baseUrl,
      userId: '',
      baseId: '',
      status: '', // 申请事项
      user: [],   // 负责人数据
      attachment: '',  // 附件
      picture: '' , // 展示图片
      validate: {}, //验证对象
      baseData: [], // 基地数据
      base: {}, // 基地查看
			ModelShow: false,
			ProjectModelShow: false,
			demandModelShow: false,
			WorkModelShow: false,
			Application: false,
			buildAgreement: false,
			projectManagement: false,
			demandModel: false,
			material: false,
			BaseTerminate:false,
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
      utotal: 0, //总条数
      upage: 1, //当前页
      usize: 10, //每页显示条数
			uploadNum: 0,
			FileUpload: [],
      FileUploads: [], // 展示图片
			curId: 1,
			isClick:false,//是否点击新增基地按钮
			curIndex:-1,//选中下标
   //   userData: this.get("userInfo")
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
	},
	mounted() {
    //加载表单验证
    this.validate = $("#baseForm").validate({
      ignore: [],
      rules: {
        name: { // 名称
          required: true,
          maxlength: 50
        },
        type: { // 类型
          required: true,
        },
        address: { // 地址
          required: true,
          maxlength: 50
        },
        unit: { // 申请单位
          required: true,
          maxlength: 50
        },
        contact: { // 联系人
          required: true,
          maxlength: 10
        },
        correspondenceAddress: { // 通讯地址
          required: true,
          maxlength: 50
        },
        theContact: { // 练习方式
          required: true,
          number: true
        },
        financialContact: { // 财务联系人
          required: true,
          maxlength: 10
        },
        contactNumber: { // 财务联系电话
          required: true,
          number: true
        },
        introduction: { // 基地简介
          required: true,
          maxlength: 1000
        },
        picture: {
          required: true,
        }

      },
      messages: {
        name: { // 名称
          required: '基地名称不能为空',
          maxlength: '最大长度为50'
        },
        type: { // 类型
          required: '请选择',
        },
        address: { // 地址
          required: '地址不能为空',
          maxlength: '最大长度为50'
        },
        unit: { // 申请单位
          required: '申请单位不能为空',
          maxlength: '最大长度为50'
        },
        contact: { // 联系人
          required: '联系人不能为空',
          maxlength: '最大长度为10'
        },
        correspondenceAddress: { // 通讯地址
          required: '通讯地址不能为空',
          maxlength: '最大长度为50'
        },
        theContact: { // 联系方式
          required: '联系方式不能为空',
          number: '电话号码只能输入数字'
        },
        financialContact: { // 财务联系人
          required: '财务联系人不能为空',
          maxlength: '最大长度为10'
        },
        contactNumber: { // 财务联系电话
          required: '财务联系电话不能为空',
          number: '电话号码只能输入数字'
        },
        introduction: { //
          required: '基地简介不能为空',
          maxlength: '基地简介最多只能输入1000个字！'
        },
        picture: {
          required: '基地展示图片不能为空!',
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
	  // 合同查看
    viewHeModel: function (obj) {
      this.obj = {};
      this.obj = obj;
      this.obj.contract_begin_time = formatData(obj.contract_begin_time);
      this.obj.contract_end_time = formatData(obj.contract_end_time);

      this.modelShowView = true;


    },

    // 查询所有用户
    getAllUser: function () {
      let that = this;

      console.log(that.upage)

      let name = $('#Searchname').val();
      that.$post(
        that.urls.getAllUser,
        {"userName": name,"pageNum": that.upage, "pageSize": that.usize }
      )
        .then(function(res) {
          if(res.success) {
            that.user = res.pageInfo.list;
            that.utotal = res.pageInfo.total;
            that.upage = res.pageInfo.pageNum;

          }
        })
    },
    // 确定负责人
    checkUserId: function (id, name) {
      let that = this;
      that.checkedUser.userId = id;
      that.checkedUser.userName = name;
      that.userId = 0,
  /*    that.userId = id;
      that.userName = name;*/
      that.isClick=false;
    },
    // 提交表单
    subForm: function () {
      let that = this;
      let flg = that.validate.form();
      that.isClick=true;
      if(flg) {

        console.log( that.picture )
       if(!that.picture || that.picture === undefined || that.picture === ''){
          layer.msg('请上传基地展示图片!', {
            time: 3000, //3s后自动关闭
          });
          return;
        }

        if(!that.userId){
          layer.msg('请选择基地负责人!', {
            time: 3000, //3s后自动关闭
          });
          return;
        }
        let   dataObj = that.combinationFormObject(that.validate.currentForm);
        dataObj.status = 1;
        dataObj.userId = that.userId; // 确定了负责人

        that.$post(
          that.urls.baseQueryHeavy,
          dataObj
        )
          .then(function(res) {
            if(res.success && res.data) {
              layer.msg(res.data, {
                time: 3000, //3s后自动关闭
              });
            }else {

              that.$post(
                that.urls.addBaseAreas,
                dataObj
              )
                .then(function(res) {
                  if(res.success) {
                    that.demandModelShow = false;
                    layer.msg('成功新增基地!', {
                      time: 3000, //3s后自动关闭
                    });
                    that.searchBase();
                  }else {
                    layer.msg('新增基地失败!', {
                      time: 3000, //3s后自动关闭
                    });
                  }
                })

            }
          })


      }
    },
		//页码切换执行方法

    obtainChild: function(data) {  //基地
      this.row = data;
    },

		pageFn: function(val, pid) {
			this.page = parseInt(val);
			this.getBase();
			//此处调列表数据接口
		}, //基地

    upageFn: function(val, pid){ //用户
      this.upage = parseInt(val);
      this.getAllUser();
    },

		/**返回子组件用户选择的一页显示条数**/ //用户
		obtainChild1: function(data) {
			this.row = data;
		},

    pageFnAtt: function(val, pid){ //用户
      this.attPage = parseInt(val);
      this.att();
    },  //材料1

    /**返回子组件用户选择的一页显示条数**/
    obtainChildAtt: function(data) {
      this.row = data;
    },


    pageFnAtt2: function(val, pid){
      this.attPage2 = parseInt(val);
      this.att2();
    },  //材料2

    /**返回子组件用户选择的一页显示条数**/
    obtainChildAtt2: function(data) {
      this.row = data;
    },


    pageFnConst: function(val, pid){
      this.constPage = parseInt(val);
      this.AllTicketss();
    },  //建设

    /**返回子组件用户选择的一页显示条数**/
    obtainChildConst: function(data) {
      this.row = data;
    },

    pageFnProject: function(val, pid){
      this.projectPage = parseInt(val);
      this.projectThe();
    },  //项目

    /**返回子组件用户选择的一页显示条数**/
    obtainChildProject: function(data) {
      this.row = data;
    },


    pageFnExperts: function(val, pid){
      this.expertsPage = parseInt(val);
      this.Model();
    },  //专家

    /**返回子组件用户选择的一页显示条数**/
    obtainChildExperts: function(data) {
      this.row = data;
    },

    pageFnExpertsLog: function(val, pid){
      this.logPage = parseInt(val);
      this.viewLog();
    },  //专家log

    /**返回子组件用户选择的一页显示条数**/
    obtainChildExpertsLog: function(data) {
      this.row = data;
    },


		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
    /**文件上传(多文件)js**/
    idFrontImg: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            console.log(names)
            console.log(urls)
            that.FileUpload = names;
            that.attachment = urls.join(";");
          }
        })
      console.log(that.FileUpload)
    },
    // 展示图片
    touchOffIdInputs(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#handIdFronts").click();
      }
    },
    /*删除附件*/
    deletetouchOffIdInputs:function(){
   		this.temFileUploads= []; // 终止附件
        this.termination= ''; // 基地终止附件
    },
    /**文件上传(多文件)js**/
    idFrontImgs: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            that.FileUploads = names;
            that.picture = urls.join(";");
            that.pictureImg = that.thatUrl + that.picture;
            that.isClick=false;
              $('#pictureImg').attr('src', that.pictureImg);
          }
        })

    },

    temtouchOffIdInputs(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#temHandIdFronts").click();
      }
    },

    // 终止附件
    /**文件上传(多文件)js**/
    temidFrontImgs: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            that.temFileUploads = names;
            that.termination = urls.join(";");
          }
        })

    },


    retouchOffIdInput(n,id) {
      this.uploadNum = n;
      this.baseId = id;
      if(n == 1) {
        $("#handIdFrontThe").click();
      }
    },

    // 制牌信息
    /**文件上传(多文件)js**/
    reservedTemidFrontImgs: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
           // let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
          //    names.push(that.getFileName(fileInfos[i].url));
            }
        //    that.temFileUploads = names;
            //that.termination = urls.join(";");
            let obj = {"id": that.baseId, "reserved": urls.join(";")}
            that.editBase(obj);


          }
        })

    },
    // 修改基地
    editBase: function (obj) {
     let that = this;
      that.$post(
        that.urls.editBaseAreas,
        obj
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('上传制牌信息成功!', {
              time: 3000, //3s后自动关闭
            });
            that.getBase();
          }else {
            layer.msg('上传制牌信息失败!', {
              time: 3000, //3s后自动关闭
            });
          }
        })
    },

		//查看基地详情
		toView:function(id){
      let that = this;
      this.base.picture="";
      console.log('基地详情id' + id)
      // 基地信息
      that.$post(
        that.urls.getBaseAreas, {
          "id": id,
        }
      )
        .then(function(res) {
          if(res.success) {
            that.base = res.data;
            let type = that.base.type;
            if(type == 0) {
              that.base.type = '现代农业综合示范基地';
            } else if(type == 1) {
              that.base.type = '现代农业特色产业基地';
            } else if(type == 2) {
              that.base.type = '乡村建设研究实践基地';
            } else if(type == 3) {
              that.base.type = '新农村建设分布式服务站';
            }
          }
        })
			this.Application = true;
		},
		//选择负责人
		projectDetails: function() {
      this.upage = 1;
			this.ProjectModelShow = true;
		},
		//新增基地模态框
		demand: function() {
      this.getAllUser();
			this.demandModelShow = true;
      this.userName = '';
      this.FileUpload = [];
      this.attachment = '';
      this.picture = ' ';
      $('#pictureImg').attr('src', ' ');
      $('#introduction').val('');
      $("#addBaseDiv input").val("");
      $('#selectType').val("");
      this.validate.resetForm();
      this.isClick = false;
		},
		//共建协议
		build:function(){
			this.buildAgreement = true;
		},
    //基地建设
    AllTicketss: function(id) {
      let that = this;
      if(id){
        that.constPage = 1;
        that.baseId = id;
      }
      that.$post(
        that.urls.getConstructionPage,
        {"baseId": that.baseId, "pageNum": that.constPage, "pageSize": 10}
      )
        .then(function(res) {
          if(res.success) {
            that.construction = res.pageInfo.list;
            that.constPage = res.pageInfo.pageNum;
            that.constTotal = res.pageInfo.total;
          }
        })
      that.ModelConstruction = true;
    },

		//对接专家团队
		Model:function(id){
      // 專家信息
      let that = this;
      if(id){
        that.expertsPage = 1;
        that.baseId = id;
      }
      that.experts = [];
      that.$post(
        that.urls.getBaseExperts, {
          "list": [that.baseId], "pageNum": that.expertsPage, "pageSize": 10
        }
      )
        .then(function(res) {
          if(res.success) {
            if(res.success){
              that.experts = res.pageInfo.list;
              that.expertsPage = res.pageInfo.pageNum;
              that.expertsTotal = res.pageInfo.total;
            }
          }
        })
			this.ModelShow = true;
		},
		//基地材料管理
		Material:function(id){
      this.curId = 1;
      this.baseId = id;
      this.attPage = 1;
      this.attPage2 = 1;
      this.att(id);
      this.att2(id);
		},

    // 材料1
    att: function () {
      let that = this;
      that.attData = [];
      that.$post(
        that.urls.getAttrPage,
        {  "pageNum": that.attPage,
          "pageSize": 10,
          "name": name ,"list": [that.baseId],"type": 1 }
      )
        .then(function(res) {
          if(res.success) {
            that.attData = res.pageInfo.list;
            that.attPage = res.pageInfo.pageNum;
            that.attTotal = res.pageInfo.total;
          }
        })

      this.material = true;
    },

    //材料2
    att2: function () {
      let that = this;
      that.tableDatap = [];
      this.material = true;
      that.$post(
        that.urls.getAttrPage,
        {  "pageNum": that.attPage2,
          "pageSize": 10,
          "name": name ,"list": [that.baseId],"type": 2}
      )
        .then(function(res) {
          if(res.success) {
            that.tableDatap = res.pageInfo.list;
            that.attPage2 = res.pageInfo.pageNum;
            that.attTotal2 = res.pageInfo.total;
          }
        })
    },


		//基地项目管理
		projectThe: function (id) {
      let that = this;
      if(id){
        that.projectPage = 1;
        that.baseId = id;
      }
      that.$post(
        that.urls.getProjectManagePageInfo,
        {"areasId": that.baseId, "screenAuditStatus": 2, "pageNum": that.projectPage, "pageSize": 10}
      )
        .then(function(res) {
          if(res.success) {
           that.projectData = res.pageInfo.list;
            that.projectTotal = res.pageInfo.total;
            that.projectPage = res.pageInfo.pageNum;
          }
        })
			this.projectManagement = true;
		},
		//基地项目管理
		management:function(obj){
      console.log(obj)
      this.curId = 1;
      this.projectObj = obj;
			this.demandModel = true;
		},
		//基地终止
		baseTerminate:function(id){
      this.baseId = id;
      this.BaseTerminate = true;
		},
    // 终止提交
    subBaseTerminate:function () {
      let that = this;
      let value = $('#terminationContent').val();
      if(!value){
        layer.msg('请输入基地终止原因!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      if(value.length > 100){
        layer.msg('内容最多只能输入100个字!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      that.$post(
        that.urls.editBaseAreas,
        {"id": that.baseId, "status": 6,"termination": that.termination,
          "terminationContent": value}
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('基地终止提交成功!', {
              time: 3000, //3s后自动关闭
            });
            that.BaseTerminate = false;
            that.getBase();
          }else {
            layer.msg('基地终止提交失败!', {
              time: 3000, //3s后自动关闭
            });
          }
        })
    },

    // 关闭基地负责人
      closeBaseT: function(){
		this.checkedUser={};
        this.ProjectModelShow = false;
      },

    // 选择基地负责人
    checkedBaseT: function(){
      let that = this;
      that.userId = that.checkedUser.userId;
      that.userName = that.checkedUser.userName;
        if(! this.userId){
          layer.msg('请选择基地负责人！', {
            time: 3000, //3s后自动关闭
          });
          return;
        }

      this.ProjectModelShow = false;
    },
    closeHeModel: function () {
      this.modelShowView = false;
    },

		//关闭模态框
		closeModel: function() {
	      this.ModelLog = false;
	      this.ModelConstruction = false;
			this.ProjectModelShow = false;
			this.demandModelShow = false;
			this.Application = false;
			this.buildAgreement = false;
			this.ModelShow = false;
			this.material = false;
			this.projectManagement = false;
			this.demandModel = false;
			this.BaseTerminate = false;


		},

    // 基地数据
    getBase: function (type, name) {
      let that = this;
     // 基地展示数据
      that.$post(
        that.urls.basePage,
        { "pageNum": that.page, "list": [1,6,2,3,5],
          "pageSize": that.size ,"type": type, "name": name }
      )
        .then(function(res) {
          if(res.success) {
            that.baseData = res.pageInfo.list;
            that.total = res.pageInfo.total;
            that.page = res.pageInfo.pageNum;
          }
        })
    },
    // 搜索
    searchBase: function () {
      let type = $('#type').val();
      let name = $('#searchRoleVal').val();
      this.getBase(type, name);
    },
    // 搜索基地负责人
    SearcUser: function () {
   this.getAllUser();
    },

    //下载资源
    downloadResources: function(filePath) {
      if(filePath != null && filePath != "") {
        window.location.href = this.urls.download + "?path=" + filePath;
      }else {
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
      }
    },
    // 跳转到预览
    toSrc: function (url) {
      console.log(url)
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open(this.thatUrl + url);
    },

    toSrcThe: function (url) {
      console.log(url)
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open( url);

    },


    subStrThe: function (str) {
      if(str) {
        if (str.length > 10) {
          let s = str.substr(0, 8);
          return s + '...';
        } else {
          return str;
        }
      }

    },

    // 附件删除
    delAtt: function () {
      this.attachment = '';
      this.FileUpload = [];
    },
    closeWorkModel: function () {
      this.ModelLog = false;
    },
    // 专家日志
    viewLog: function (id) {
      let that = this;
      that.ModelLog = true;
       if(id){
         that.logPage = 1;
         that.logId = id;
       }
      that.logData = [];
      that.$post(
        that.urls.getLogPage,
        {"pageNum": that.logPage,
          "pageSize": 10, "projectId": that.logId}
      )
        .then(function(res) {
          if(res.success) {
            that.logData = res.pageInfo.list;
            that.logPage = res.pageInfo.pageNum;
            that.logTotal = res.pageInfo.total;
          }
        })

    },
    // 关闭项目详情
    closeDemandModel: function () {
      this.demandModel = false;
    },

    // 模板下砸
    getBaseTem: function () {
      let that = this;
      that.$post(
        that.urls.getTemplate,
        {"type": 1}
      )
        .then(function(res) {
          if(res.success) {

            that.downloadResources(res.data.url);
          }else {
            layer.msg('请联系管理员上传【基地申请书】', {
              time: 3000, //3s后自动关闭
            });
          }
        })

    },
	}
}
